package com.yy.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yy.service.ProviderHystrixService;
import com.yy.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author young
 * @date 2022/12/25 17:07
 * &#064;description:  全局配置服务降级
 */
@Slf4j
@RestController
@RequestMapping("global")
@DefaultProperties(defaultFallback = "global")
public class GlobalHystrixController {
    @Resource
    private ProviderHystrixService providerHystrixService;

    @GetMapping("/test/ok/{id}")
    public Result<String> providerOk(@PathVariable("id") Integer id){
        return providerHystrixService.selectOne(id);
    }


    //全局配置服务降级
    @HystrixCommand
    @GetMapping("/test/wait/{id}")
    public Result<String> providerWait(@PathVariable("id") Integer id){
        return providerHystrixService.selectOneWait(id);
    }

    /**
     * 出现异常可以进行全局处理，成功跳转到全局处理方法
     * @return
     */
    @HystrixCommand
    @GetMapping("ee")
    public Result<String> er(){
        int a = 10/0;
        return  Result.ok("不行");
    }

    /**
     * 返回类型必须与之前的服务接口返回类型一致Result<T>，否则报错！
     * @return
     */
    public Result<String> global(){
        return Result.ok("全局服务降级配置效果，消费者端扛不住了，要开始降级处理了");
    }
}
